package f.f0.i;

import c.j.a.c.y.a.i;
import f.a0;
import f.c0;
import f.f0.f.g;
import f.f0.g.c;
import f.q;
import f.v;
import f.w;
import f.y;
import g.e;
import g.h;
import g.k;
import g.u;
import g.z;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/* compiled from: Http2Codec */
public final class f implements c {
    public static final List<String> f = f.f0.c.q("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");
    public static final List<String> g = f.f0.c.q("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");
    public final f.s.a a;
    public final g b;
    public final g c;
    public p d;
    public final w e;

    /* compiled from: Http2Codec */
    public class a extends k {
        public boolean b = false;
        public long c = 0;

        public a(z zVar) {
            super(zVar);
        }

        public final void a(IOException iOException) {
            if (!this.b) {
                this.b = true;
                f fVar = f.this;
                fVar.b.i(false, fVar, this.c, iOException);
            }
        }

        public long c(e eVar, long j) throws IOException {
            try {
                long c = this.a.c(eVar, j);
                if (c > 0) {
                    this.c += c;
                }
                return c;
            } catch (IOException e) {
                a(e);
                throw e;
            }
        }

        public void close() throws IOException {
            this.a.close();
            a(null);
        }
    }

    public f(v vVar, f.s.a aVar, g gVar, g gVar2) {
        this.a = aVar;
        this.b = gVar;
        this.c = gVar2;
        List list = vVar.c;
        w wVar = w.H2_PRIOR_KNOWLEDGE;
        if (!list.contains(wVar)) {
            wVar = w.HTTP_2;
        }
        this.e = wVar;
    }

    public void a() throws IOException {
        ((f.f0.i.p.a) this.d.f()).close();
    }

    public void b(y yVar) throws IOException {
        if (this.d == null) {
            p pVar;
            Object obj = null;
            int i = yVar.d != null ? 1 : 0;
            q qVar = yVar.c;
            ArrayList arrayList = new ArrayList(qVar.f() + 4);
            arrayList.add(new c(c.f, yVar.b));
            arrayList.add(new c(c.g, i.F(yVar.a)));
            String c = yVar.c.c("Host");
            if (c != null) {
                arrayList.add(new c(c.i, c));
            }
            arrayList.add(new c(c.h, yVar.a.a));
            int f = qVar.f();
            for (int i2 = 0; i2 < f; i2++) {
                h e = h.e(qVar.d(i2).toLowerCase(Locale.US));
                if (!f.contains(e.w())) {
                    arrayList.add(new c(e, qVar.g(i2)));
                }
            }
            g gVar = this.c;
            boolean z = i ^ 1;
            synchronized (gVar.r) {
                int i3;
                synchronized (gVar) {
                    if (gVar.f > 1073741823) {
                        gVar.t(b.REFUSED_STREAM);
                    }
                    if (gVar.g) {
                        throw new a();
                    }
                    i3 = gVar.f;
                    gVar.f = i3 + 2;
                    pVar = new p(i3, gVar, z, false, null);
                    if (i == 0 || gVar.m == 0 || pVar.b == 0) {
                        obj = 1;
                    }
                    if (pVar.h()) {
                        gVar.c.put(Integer.valueOf(i3), pVar);
                    }
                }
                q qVar2 = gVar.r;
                synchronized (qVar2) {
                    if (qVar2.e) {
                        throw new IOException("closed");
                    }
                    qVar2.q(z, i3, arrayList);
                }
            }
            if (obj != null) {
                gVar.r.flush();
            }
            this.d = pVar;
            p.c cVar = pVar.i;
            long j = (long) ((f.f0.g.f) this.a).j;
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            cVar.g(j, timeUnit);
            this.d.j.g((long) ((f.f0.g.f) this.a).k, timeUnit);
        }
    }

    public c0 c(a0 a0Var) throws IOException {
        Objects.requireNonNull(this.b.f);
        String c = a0Var.f.c("Content-Type");
        if (c == null) {
            c = null;
        }
        long a = f.f0.g.e.a(a0Var);
        a aVar = new a(this.d.g);
        Logger logger = g.q.a;
        return new f.f0.g.g(c, a, new u(aVar));
    }

    public void cancel() {
        p pVar = this.d;
        if (pVar != null) {
            pVar.e(b.CANCEL);
        }
    }

    public void d() throws IOException {
        this.c.r.flush();
    }

    public g.y e(y yVar, long j) {
        return this.d.f();
    }

    public f.a0.a f(boolean z) throws IOException {
        q qVar;
        p pVar = this.d;
        synchronized (pVar) {
            pVar.i.i();
            while (pVar.e.isEmpty() && pVar.k == null) {
                try {
                    pVar.j();
                } finally {
                    pVar.i.n();
                }
            }
            if (pVar.e.isEmpty()) {
                throw new u(pVar.k);
            }
            qVar = (q) pVar.e.removeFirst();
        }
        w wVar = this.e;
        ArrayList arrayList = new ArrayList(20);
        int f = qVar.f();
        f.f0.g.i iVar = null;
        for (int i = 0; i < f; i++) {
            String d = qVar.d(i);
            String g = qVar.g(i);
            if (d.equals(":status")) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("HTTP/1.1 ");
                stringBuilder.append(g);
                iVar = f.f0.g.i.a(stringBuilder.toString());
            } else if (!g.contains(d)) {
                Objects.requireNonNull((f.v.a) f.f0.a.a);
                arrayList.add(d);
                arrayList.add(g.trim());
            }
        }
        if (iVar != null) {
            f.a0.a aVar = new f.a0.a();
            aVar.b = wVar;
            aVar.c = iVar.b;
            aVar.d = iVar.c;
            String[] strArr = (String[]) arrayList.toArray(new String[arrayList.size()]);
            f.q.a aVar2 = new f.q.a();
            Collections.addAll(aVar2.a, strArr);
            aVar.f = aVar2;
            if (z) {
                Objects.requireNonNull((f.v.a) f.f0.a.a);
                if (aVar.c == 100) {
                    return null;
                }
            }
            return aVar;
        }
        throw new ProtocolException("Expected ':status' header not present");
    }
}
